﻿//    Copyright 2010,2011 Himanshu Vasishth
//   
//    This file is part of SecretVault.
//
//    SecretVault is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SecretVault is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with SecretVault.  If not, see <http://www.gnu.org/licenses/>.
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Layout;
using PasswordStoreProvider;

namespace PasswordLocker.Controls
{
    /// <summary>
    /// UI control used to display a login info
    /// </summary>
    public partial class LoginInfoControl : UserControl
    {
        private bool mouseOnControl;
        private LoginInfo loginInfo;
        private bool buttonsVisible;

        public LoginInfoControl()
        {
            InitializeComponent();
            urlLabel.Font = Fonts.URLFont;
            loginLabel.Font = Fonts.LoginFont;
            panel1.BackColor = Color.Transparent;
            BackColor = Colors.LoginInfoBackground;

            PasswordVisible = false;
            ButtonsVisible = false;

            changeButton.Text = Resources.ChangePasswordText;
            showButton.Text = Resources.ShowPasswordText;
            deleteButton.Text = Resources.DeleteLoginInfoText;

            // Start listening to mouse enter and leave on the control
            var mouseMessageFilter = MouseMessagesFilter.Instance;
            mouseMessageFilter.WatchControl(this,
                                            LoginDisplayControl_MouseEnter,
                                            LoginDisplayControl_MouseLeave);
            Disposed += (s, e) => mouseMessageFilter.StopWatchingControl(this);
        }


        public event LoginInfoEvent ShowPasswordClicked;
        public event LoginInfoEvent DeleteInfoClicked;
        public event LoginInfoEvent ChangeInfoClicked;

        /// <summary>
        /// The login info displayed by the control
        /// </summary>
        public LoginInfo LoginInfo
        {
            get
            {
                return loginInfo;
            }
            set
            {
                loginInfo = value;
                if (value != null)
                {
                    urlLabel.Text = value.Resource;
                    loginLabel.Text = value.Login;
                }
                else
                {
                    urlLabel.Text = string.Empty;
                    loginLabel.Text = string.Empty;
                }
            }
        }

        /// <summary>
        /// Displays password for 10 secs
        /// </summary>
        public void ShowPassword(string password)
        {
            passwordLabel.Text = password;
            // start a timer to hide the password in 10 secs
            var hidePasswordTimer = new Timer();
            PasswordVisible = true;
            hidePasswordTimer.Interval = 10000;
            hidePasswordTimer.Tick += (s, arg) =>
                                          {
                                              hidePasswordTimer.Dispose();
                                              PasswordVisible = false;
                                              passwordLabel.Text = string.Empty;
                                          };
            hidePasswordTimer.Start();
        }

        private bool ButtonsVisible
        {
            get
            {
                return buttonsVisible;
            }
            set
            {
                buttonsVisible = value;
                showButton.Visible = value ? !PasswordVisible : false;
                deleteButton.Visible = value;
                changeButton.Visible = value;
            }
        }

        private bool PasswordVisible
        {
            get
            {
                return passwordLabel.Visible;
            }
            set
            {
                passwordLabel.Visible = value;
                showButton.Visible = value ? false : ButtonsVisible;
            }
        }

        private void LoginDisplayControl_MouseEnter(object sender, EventArgs e)
        {
            mouseOnControl = true;
            var showButtonTimer = new Timer();
            showButtonTimer.Interval = 1000;
            showButtonTimer.Tick += (s, eargs) =>
                                        {
                                            ButtonsVisible = mouseOnControl;
                                            showButtonTimer.Dispose();
                                        };
            showButtonTimer.Start();
        }

        private void LoginDisplayControl_MouseLeave(object sender, EventArgs e)
        {
            mouseOnControl = false;
            ButtonsVisible = false;
        }

        private void ShowButton_MouseClick(object sender, MouseEventArgs e)
        {
            if(ShowPasswordClicked != null)
            {
                ShowPasswordClicked(LoginInfo);
            }
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            if(DeleteInfoClicked != null)
            {
                DeleteInfoClicked(LoginInfo);
            }
        }

        private void ChangeButton_Click(object sender, EventArgs e)
        {
            if (ChangeInfoClicked != null)
            {
                ChangeInfoClicked(LoginInfo);
            }
        }
    }
}
